<?php
  if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */
 global $login_status,$user_id,$is_admin,$action;
 function add_pdf(){
    global $event_id,$db;
    $event_title=get_event_info($event_id, 'event_title');
    $select_media=select_media_file('file_id', '','pdf');
    $select_type=select_filetype('file_type', '');
    echo "<form id=\"add_file1\" action=\"?mod=panel&file=livepdf\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"add_file_process\"/>
    <input type=\"hidden\" name=\"file_type\" value=\"pdf\"/>
    <h4>Menambahkan File untuk $event_title</h4>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td><input type=\"hidden\" name=\"event_id\" value=\"$event_id\"/> </td></tr>
<tr><td>Nama File</td></tr><tr><td>$select_media </td></tr>
<tr><td><input type=\"submit\" value=\"Tambahkan ke Event\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
}
 function show_live_event(){
     global $db,$user_id,$login_status;
     if (!empty($user_id)){
     $sql="select id,event_title,event_day,event_time_start,event_time_finish
          from _event_events 
          where NOW() BETWEEN event_time_start and event_time_finish
          and event_creator=$user_id
          order by event_time_start desc";
    $result=$db->query($sql);
    $nrow=mysql_num_rows($result); 
    if ($nrow>0){
        echo "<h2>Live Event</h2>";
        while ($row=$db->fetch_array($result)){
            extract($row);
            $time_start=explode(" ",$event_time_start);
            $time_finish=explode(" ",$event_time_finish);
            if (strlen($event_title)>50){
                $title=substr($event_title,0,50)."...";
            } else $title=$event_title;
            $title=strtoupper($title);
            $hari=date("l",strtotime($event_time_start));
            echo "<div class=\"event_feed_rows\">
            [".trim($event_day)."] 
                    <a href=\"?mod=panel&file=livepdf&action=control&event_id=$id\" 
                    title=\"$event_title\">$title</a> ";
            if (!checkArchiveExist($id)){
                echo "<a href=\"?mod=panel&file=livepdf&action=add_file&event_id=$id\" 
                    title=\"$event_title\">$title</a>";
            }          
            echo "</div>";
        }
    } else {
        echo "<h2>There is No Live Event!</h2>";
    }   
     }  
 }
function add_file_process(){
    global $db,$event_id,$file_id,$file_type;
    $sql="insert into _event_archives values (null,'$event_id','$file_id','$file_type')";
    $db->query($sql);
} 
function doc_control(){
    global $db,$event_id,$is_admin;
    if (!empty ($event_id)){
    $sql="select a.*,b.filename
          from _event_archives a,_media_file b
          where a.file_id=b.id and a.event_id=$event_id";
    $result=$db->query($sql);
    $nrow=mysql_num_rows($result);
    if ($nrow>0){
        //$p=floor(word_count()/$pagesize);
        echo "<h2>List of Media Archive</h2>
            <div><a href=\"javascript:history.go(-1);\">
            <img src=\"images/back_button.png\" title=\"Back\"/></a></div>
            <table width=\"100%\" cellspacing=0><tr><th>No</th>
            <th>Filename</th><th>Action</th></tr>";
        $i=0;
        while ($row=$db->fetch_array($result)){
            extract($row);
            $i++;
            echo "<tr><td align='center'>$i</td>
            <td><a href=\"activepdf.php?sourcepdf=$filename\" target=\"_blank\">$filename</a></td><td align='center'>
            <a href=\"?mod=events&action=edit&file=archive&archive_id=$id\">
            <img class='icon' src=\"images/edit.png\" title=\"Edit Archive\"/></a>
            <a href=\"?mod=events&action=delete&file=archive&archive_id=$id\">
            <img class='icon' src=\"images/delete.png\" title=\"Delete Archive\"/></a>
            </td></tr>";
        }
        echo "</table>
            <div><a href=\"javascript:history.go(-1);\">
            <img src=\"images/back_button.png\" title=\"Back\"/></a></div>";
         
        } else {
            echo "No archive for this event";
            if ($is_admin=='1'){
                echo "<a href=\"?mod=events&action=add&file=archive&event_id=$event_id\">
            <img class='icon' src=\"images/add.png\" title=\"Add Archive\"/></a>";
            }
        }
    }
}
switch($action){
    case 'control':
        doc_control();
    case 'add_file_process':
        add_file_process();
        break;
    case 'add_file':
        add_pdf();
        break;
    default:
     show_live_event();
}     
?>
